package com.agileboot.domain.medicine.model;

import cn.hutool.core.bean.BeanUtil;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.domain.medicine.command.MedicineCommand;
import com.agileboot.domain.medicine.db.Medicine;
import com.agileboot.domain.medicine.db.MedicineService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class MedicineModel extends Medicine {

    private MedicineService medicineService;

    public MedicineModel(MedicineService medicineService) {
        this.medicineService=medicineService;
    }

    public MedicineModel(Medicine medicine,MedicineService medicineService) {
        this(medicineService);
        if (medicine!=null){
            BeanUtil.copyProperties(medicine, this);
        }
    }


    public void loadAddCommand(MedicineCommand command) {
        if (command != null) {
            BeanUtil.copyProperties(command, this);
        }
    }

    public void loadUpdateCommand(MedicineCommand command) {
        if (command != null) {
            loadAddCommand(command);
        }
    }

    public void checkMedicineIsUnique() {
        if (medicineService.isMedicineNameDuplicated(getName())) {
            throw new ApiException(ErrorCode.Business.MEDICINE_NAME_IS_NOT_UNIQUE);
        }
    }

    public void checkMedicineIsEmpty(MedicineModel medicineModel) {
        if (medicineModel == null) {
            throw new ApiException(ErrorCode.Business.MEDICINE_IS_EMPTY);
        }
    }

}
